package com.fahai.web.controller;

import com.fahai.common.PageVo;
import com.fahai.utils.json.JsonResult;
import com.fahai.web.service.CallRecordService;
import com.fahai.web.vo.CallRecordVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * Created by sky on 17/5/23.
 * 通话记录的控制器
 */


@Controller
@RequestMapping("/call")
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class CallRecordController extends BaseController {
    private static Logger logger = LoggerFactory.getLogger(CallRecordController.class);

    @Autowired
    CallRecordService callRecordService;

    /**
     * 返回界面
     * @param model
     * @return
     */
    @PreAuthorize("hasAnyAuthority('ADMIN','咨询管理-0','咨询管理-1')")
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public String enterCallPage(Model model){
        model.addAttribute("selectTab","咨询管理");
        return "call_manage";
    }

    /**
     * 查看通话记录列表
     * @param pageVo
     * @return
     */
    @PreAuthorize("hasAnyAuthority('ADMIN','咨询管理-0','咨询管理-1')")
    @ResponseBody
    @RequestMapping(value = "/recordList",method = RequestMethod.POST)
    public JsonResult findCallRecordList(PageVo pageVo, Model model,CallRecordVo callRecordVo){
        PageVo pageVoData = callRecordService.findCallRecordList(callRecordVo, pageVo);
        return ok("获取通话列表数据",pageVoData);
    }

    /**
     * 分页总数
     *
     * @param model
     * @return
     * @throws IOException
     */
    @PreAuthorize("hasAnyAuthority('ADMIN','咨询管理-0','咨询管理-1')")
    @ResponseBody
    @RequestMapping(value = "/calllistcount", method = RequestMethod.POST)
    public JsonResult listcount(PageVo pageVo, Model model, CallRecordVo callRecordVo) throws IOException {
        callRecordVo.setOrderBy(null);
        int count= callRecordService.getCount(callRecordVo, pageVo);
        pageVo.setTotal( Long.valueOf(count));
        model.addAttribute("count", pageVo);
        return ok("查询", pageVo.getTotal());
    }




    /**
     * 查看电话图表
     * @param createTime
     * @return
     */
    @PreAuthorize("hasAnyAuthority('ADMIN','咨询管理-0','咨询管理-1')")
    @RequestMapping(value = "/getCallChart",method = RequestMethod.GET)
    public @ResponseBody
    JsonResult getUserChart(String createTime){

        Map<String,List<Map<String,Object>>>  getData=callRecordService.selectChartForHours(createTime);

        return ok("查询成功",getData);
    }

}
